package com.ic.loan.activity

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.gyf.immersionbar.ktx.immersionBar
import com.ic.loan.R
import com.ic.loan.bean.MyWallet
import com.ic.loan.http.ApiUrl
import com.ic.loan.http.HttpUtils
import com.ic.loan.http.ResultListener
import com.ic.loan.pop.AuditPop
import com.ic.loan.pop.RedEnvelopePop
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.activity_why_choose_us.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import android.content.Intent
import android.net.Uri
import com.blankj.utilcode.util.*
import com.gyf.immersionbar.ImmersionBar
import com.ic.loan.bean.KeFu
import com.ic.loan.cache.Constant
import java.lang.Exception


class WhyChooseUsActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        immersionBar {
            statusBarDarkFont(false)
        }

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_why_choose_us)
        ImmersionBar.setStatusBarView(this,sp_status_bar)
        tvUpdateApprovalStatus.setOnClickListener {
            getMywallet()
        }
        tvCustomerService.setOnClickListener {
            getKeFu()
        }
        ivLogout.setOnClickListener {
            XPopup.Builder(this).asConfirm(
                StringUtils.getString(R.string.tips),
                StringUtils.getString(R.string.tips_content),
                StringUtils.getString(R.string.cancel),
                StringUtils.getString(R.string.yes), {
                    logout()
                }, {}, false
            ).show()
        }
        getMywallet()
    }

    private fun getKeFu() {
        showLoading()
        HttpUtils.postOkGo(mutableMapOf("timestamp" to System.currentTimeMillis() / 1000),ApiUrl.POST_KEFU,object : ResultListener() {
            override fun onSucceeded(json: String?) {
                try {
                    val kefu = GsonUtils.fromJson<KeFu>(json,KeFu::class.java)
                    val uri = Uri.parse("http://Wa.me/${kefu.kefu}")
                    val intent = Intent(Intent.ACTION_VIEW, uri)
                    startActivity(intent)
                }catch (e:Exception){

                }
            }

            override fun onLoadFinish() {
                hintLoading()
            }
        })
    }

    //获取钱包接口
    private fun getMywallet(){
        showLoading()
        HttpUtils.postOkGo(mutableMapOf("timestamp" to System.currentTimeMillis() / 1000),ApiUrl.POST_USER_MYWALLET,object : ResultListener() {
            override fun onSucceeded(json: String?) {
                val myWallet = GsonUtils.fromJson(json,MyWallet::class.java)
                if (myWallet.app_amount.toDouble() > 0.0){
                    XPopup.Builder(this@WhyChooseUsActivity).asCustom(RedEnvelopePop(this@WhyChooseUsActivity) {
                        ActivityUtils.startActivity(MainActivity::class.java)
                        onBackPressed()
                    }).show()
                }else{
                    XPopup.Builder(this@WhyChooseUsActivity).asCustom(AuditPop(this@WhyChooseUsActivity)).show()
                }
            }

            override fun onLoadFinish() {
                hintLoading()
            }
        })
    }

    private fun logout() {
        showLoading()
        HttpUtils.postOkGo(
            mutableMapOf("timestamp" to System.currentTimeMillis() / 1000),
            ApiUrl.POST_LOGOUT,
            object : ResultListener() {
                override fun onSucceeded(json: String?) {
                    SPUtils.getInstance().remove(Constant.TOKEN)
                    SPUtils.getInstance().remove(Constant.APPLY_SUCCESS)
                    onBackPressed()
                    ActivityUtils.startActivity(LoginActivity::class.java)
                }

                override fun onLoadFinish() {
                    super.onLoadFinish()
                    hintLoading()
                }
            })
    }

}